package com.cbtx.module_sp.ui.vm

import android.app.Application
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.cbtx.module.base.ContextManager
import com.cbtx.module.base.SpUtils
import com.cbtx.module.base.http.HttpManager
import com.cbtx.module.base.http.MyRequestCallBack
import com.cbtx.module.base.vm.BaseVM
import com.cbtx.module.common.bean.request.StoreInfoEntity
import com.cbtx.module.common.http.HttpUrlManager
import com.cbtx.module_sp.MachineFactory
import com.cbtx.module_sp.MyApplication
import com.cbtx.module_sp.R
import com.cbtx.module_sp.bean.IMachine
import com.cbtx.module_sp.interceptor.HeaderInterceptor
import com.txcb.lib.base.utils.ZxingUtils

class DeviceInfoVM(application: Application) : BaseVM(application) {

    val storeInfoEntity: MutableLiveData<StoreInfoEntity> = MutableLiveData()

    fun ubindQrCode(): Bitmap? {
        val wh: Int = ContextManager.getContext().resources.getDimensionPixelSize(R.dimen.dp_400)
        val iMachine: IMachine =
            MachineFactory.getAutoCreate(MyApplication.instance)
        val url: String = ContextManager.getContext().getString(
            R.string.register_qr,
            "ad.mjcmkj.com",
            if (TextUtils.isEmpty(Build.getSerial())) "" else Build.getSerial(),
            MachineFactory.getAutoCreate(ContextManager.getContext()).uniqueDeviceCode,
            "INTERACTIVE",
            iMachine.getLocalMac(),
            iMachine.getModel()
        )
        return ZxingUtils.createQRCode(
            url,
            wh,
            BitmapFactory.decodeResource(mApplication.resources, R.drawable.ic_logo_qr_code)
        )
    }

    fun getStoreInfo() {
        var url = HttpUrlManager.GET_STORE_INFO + "?lng=" + 0.0 + "&lat=" + 0.0
        HttpManager.getInstance("", HeaderInterceptor(MyApplication.instance.getHeaderMap()))
            .sendPost(
                url,
                HashMap(),
                object : MyRequestCallBack<StoreInfoEntity>() {
                    override fun onSuccess(obj: StoreInfoEntity) {
                        storeInfoEntity.value = obj
                        SpUtils.putString("storeName",obj.data.storeName)
                        SpUtils.putString("roomCode",obj.data.roomCode)
                        SpUtils.putInt("storeId",obj.data.storeId)
                    }

                    override fun onFailure(error: Int, msg: String?) {

                    }
                })
    }
}